/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.spend.repository;

import me.zhengjie.modules.spend.domain.Spend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2022-04-08
**/
public interface SpendRepository extends JpaRepository<Spend, String>, JpaSpecificationExecutor<Spend> {


    @Query(value="SELECT sum(price)  from spend where left(spend_time,4) = ?1",nativeQuery=true)
    Integer getYearPriceSum(String year);

    @Query(value="SELECT sum(price) as sum ,count(price) as count from spend where left(spend_time,7) = ?1",nativeQuery=true)
    Map<Object,Object> number(String dateOne);

    @Query(value="SELECT count(1)  from spend where spend_type = ?1",nativeQuery=true)
    Integer getSpendTypeNum(String spendData);

    @Query(value="SELECT sum(price)  from spend where spend_type = ?1",nativeQuery=true)
    Integer getSpendSum(String spendData);

    @Query(value="SELECT count(1)  from spend where pay_type = ?1",nativeQuery=true)
    Integer getPayTypeNum(String payData);

    @Query(value="SELECT sum(price)  from spend where pay_type = ?1",nativeQuery=true)
    Integer getPaySum(String payData);

    @Query(value="SELECT *  from spend order by spend_time desc ",nativeQuery=true)
    List<Spend> getAll();

    /*获取花费总金额*/
    @Query(value="SELECT sum(price) from spend",nativeQuery=true)
    Integer totalSpend();


    @Query(value="SELECT * from spend where left(spend_time,7) = ?1",nativeQuery=true)
    List<Map<Object,Object>>getMonthDate(String spendTime);


    @Query(value = " SELECT LEFT (spend_time, 10) date, sum(price) number FROM spend " +
            " WHERE LEFT (spend_time, 10) BETWEEN ?1 AND ?2 " +
            " GROUP BY LEFT (spend_time, 10) ", nativeQuery = true)
    List<Map<String, String>> getAllByDate(String startTime, String endTime);
}